<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />

    

    
    <title>Java之Collection学习笔记 | welcome</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="keywords" content="Collection,List,Set,泛型" />
    
    <meta name="description" content="CollectionInterface Collection 来自java.util包下，是非常实用的数据结构，字面意思是容器。 JDK中的描述：它是集合层次结构根接口。一个集合代表一组对象，被称为元素。一些集合允许重复的元素，而其他的则不允许。有些是有序和无序的人。JDK没有提供这个接口的任何直接的实现：它提供更具体的子接口像  Set和 List实现。此接口通常用于通过收集和操纵他们最大的共">
<meta property="og:type" content="article">
<meta property="og:title" content="Java之Collection学习笔记">
<meta property="og:url" content="https://hchdream.gitee.io/hexo/2020/05/14/Collection%E9%9B%86%E5%90%88%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E5%8F%8A%E4%BB%A3%E7%A0%81/index.html">
<meta property="og:site_name" content="welcome">
<meta property="og:description" content="CollectionInterface Collection 来自java.util包下，是非常实用的数据结构，字面意思是容器。 JDK中的描述：它是集合层次结构根接口。一个集合代表一组对象，被称为元素。一些集合允许重复的元素，而其他的则不允许。有些是有序和无序的人。JDK没有提供这个接口的任何直接的实现：它提供更具体的子接口像  Set和 List实现。此接口通常用于通过收集和操纵他们最大的共">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://hchdream.gitee.io/images/img/portfolio/04-large.jpg">
<meta property="article:published_time" content="2020-05-13T16:00:00.000Z">
<meta property="article:modified_time" content="2020-10-28T08:34:47.543Z">
<meta property="article:author" content="XiaoHuang">
<meta property="article:tag" content="Collection">
<meta property="article:tag" content="List">
<meta property="article:tag" content="Set">
<meta property="article:tag" content="泛型">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://hchdream.gitee.io/images/img/portfolio/04-large.jpg">
    

    
        <link rel="alternate" href="/atom.xml" title="welcome" type="application/atom+xml" />
    

    
        <link rel="icon" href="/hexo/css/images/favicon.ico" />
    

    
<link rel="stylesheet" href="/hexo/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/hexo/libs/titillium-web/styles.css">

    
<link rel="stylesheet" href="/hexo/libs/source-code-pro/styles.css">


    
<link rel="stylesheet" href="/hexo/css/style.css">


    
<script src="/hexo/libs/jquery/3.5.0/jquery.min.js"></script>

    
    
        
<link rel="stylesheet" href="/hexo/libs/lightgallery/css/lightgallery.min.css">

    
    
        
<link rel="stylesheet" href="/hexo/libs/justified-gallery/justifiedGallery.min.css">

    
    
    
    


<meta name="generator" content="Hexo 5.2.0"></head>

<body>
    <div id="wrap">
        <header id="header">
    <div id="header-outer" class="outer">
        <div class="container">
            <div class="container-inner">
                <div id="header-title">
                    <h1 class="logo-wrap">
                        <a href="/hexo/" class="logo"></a>
                    </h1>
                    
                </div>
                <div id="header-inner" class="nav-container">
                    <a id="main-nav-toggle" class="nav-icon fa fa-bars"></a>
                    <div class="nav-container-inner">
                        <ul id="main-nav">
                            
                                <li class="main-nav-list-item" >
                                    <a class="main-nav-list-link" href="/hexo/">主页</a>
                                </li>
                            
                                    <ul class="main-nav-list"><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/Java/">Java</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/Spring/">Spring</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/nginx/">nginx</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/springcloud/">springcloud</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F/">镜像加速</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/hexo/categories/%E9%A1%B9%E7%9B%AE/">项目</a></li></ul>
                                
                                <li class="main-nav-list-item" >
                                    <a class="main-nav-list-link" href="/hexo/about/index.html">关于</a>
                                </li>
                            
                        </ul>
                        <nav id="sub-nav">
                            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/hexo/',
        CONTENT_URL: '/hexo/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/hexo/js/insight.js"></script>


</div>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
</header>

        <div class="container">
            <div class="main-body container-inner">
                <div class="main-body-inner">
                    <section id="main">
                        <div class="main-body-header">
    <h1 class="header">
    
    <a class="page-title-link" href="/hexo/categories/Java/">Java</a>
    </h1>
</div>

                        <div class="main-body-content">
                            <article id="post-Collection集合学习笔记及代码" class="article article-single article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
            <header class="article-header">
                
    
        <h1 class="article-title" itemprop="name">
        Java之Collection学习笔记
        </h1>
    

            </header>
        
        
            <div class="article-meta">
                
  <div class="article-date">
    <i class="fa fa-calendar"></i>
    <a href="/hexo/2020/05/14/Collection%E9%9B%86%E5%90%88%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E5%8F%8A%E4%BB%A3%E7%A0%81/" class="article-date">
       <time datetime="2020-05-13T16:00:00.000Z" itemprop="datePublished">2020-05-14</time>
    </a>
  </div>


<div class="article-date">
  <i class="fa fa-calendar-plus-o"></i>
  <a href="/hexo/2020/05/14/Collection%E9%9B%86%E5%90%88%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E5%8F%8A%E4%BB%A3%E7%A0%81/" class="article-date">
     <time datetime="2020-10-28T08:34:47.543Z" itemprop="dateModified">2020-10-28</time>
  </a>
</div>


                

                
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link-link" href="/hexo/tags/Collection/" rel="tag">Collection</a>, <a class="tag-link-link" href="/hexo/tags/List/" rel="tag">List</a>, <a class="tag-link-link" href="/hexo/tags/Set/" rel="tag">Set</a>, <a class="tag-link-link" href="/hexo/tags/%E6%B3%9B%E5%9E%8B/" rel="tag">泛型</a>
    </div>

                

                

            </div>
        
        
        <div class="article-entry" itemprop="articleBody">
            

            

            

            <p><img src="https://hchdream.gitee.io/images/img/portfolio/04-large.jpg"></p>
<h1 id="Collection"><a href="#Collection" class="headerlink" title="Collection"></a>Collection</h1><p>Interface Collection<E> 来自java.util包下，是非常实用的数据结构，字面意思是容器。</E></p>
<p>JDK中的描述：它是集合层次结构根接口。一个集合代表一组对象，被称为元素。一些集合允许重复的元素，而其他的则不允许。有些是有序和无序的人。JDK没有提供这个接口的任何直接的实现：它提供更具体的子接口像  <code>Set</code>和 <code>List</code>实现。此接口通常用于通过收集和操纵他们最大的共性是理想。</p>
<p><strong>Collection的继承实现体系图</strong></p>
<p><strong>Collection</strong></p>
<p>子接口：</p>
<p>​     |– <strong>List</strong>：特点：元素是有序的（怎么存入，怎么取出），元素可以重复。因为该集合体系有索引       </p>
<p>​                常用实现类：</p>
<p>​                 <strong>ArrayList</strong>:底层的数据结构使用的是数组结构    特点：查询速度快，但是添加和删除元素慢，线程                                            不同步                  </p>
<p>​                <strong>LinkedList</strong>：底层使用的是链表数据结构      特点：查询速度慢，增删速度快</p>
<p>​                 <strong>Vector</strong>：底层是数组数据结构，Vector是线程同步的，无论增删还是查询都慢</p>
<p>​    |–<strong>Set</strong>：元素是无序的，元素不重复</p>
<h2 id="List"><a href="#List" class="headerlink" title="List"></a>List</h2><p>List特有方法，凡是可以操作角标的方法都是体系特有的方法</p>
<ul>
<li><p>增</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">add(index,element)	<span class="comment">//将指定的元素到这个列表的末尾（可选操作）。</span></span><br><span class="line">addAll(index,Collection)	<span class="comment">//追加指定集合的所有元素到这个列表的末尾，按他们的指定集合的迭代器返回 </span></span><br></pre></td></tr></table></figure>
</li>
<li><p>删</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">remove(index）	<span class="comment">//移除此列表中指定位置的元素</span></span><br></pre></td></tr></table></figure>

<ul>
<li>改</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set(index,element)	<span class="comment">//用指定元素替换此列表中指定位置的元素</span></span><br></pre></td></tr></table></figure>

<ul>
<li>查</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">get(index)		<span class="comment">//返回此列表中指定位置的元素</span></span><br><span class="line"><span class="function">List&lt;E&gt; <span class="title">subList</span><span class="params">(<span class="keyword">int</span> fromIndex, <span class="keyword">int</span> toIndex)</span> 	<span class="comment">//返回一个子集合，指定头尾角标</span></span></span><br><span class="line"><span class="function">Iterator&lt;E&gt; <span class="title">listIterator</span><span class="params">()</span>	<span class="comment">//返回列表元素的列表迭代器</span></span></span><br></pre></td></tr></table></figure>

<p>List集合特有的迭代器：<strong>ListIterator</strong>是<strong>Iterator</strong>的子接口，在迭代时，不可以通过集合对象的方法操作集合中的元素，会发生并发异常， <strong>ConcurrentModificationException</strong> 所以在迭代时，只能用迭代器的方法操作元素，Iterator的方法有局限，只能对元素取出判断和删除的操作， <strong>ListIterator</strong>在其基础上加了添加、修改等操作，该接口通过List的<strong>listIterator</strong> 方法获取</p>
<p>一个测试List集合的Demo</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ListDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ArrayList arrayList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        arrayList.add(<span class="string">&quot;java1&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;java2&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;java3&quot;</span>);</span><br><span class="line">        <span class="comment">//在迭代过程中准备添加或者删除元素</span></span><br><span class="line">       <span class="comment">/* Iterator it = arrayList.iterator();</span></span><br><span class="line"><span class="comment">        while (it.hasNext()) &#123;</span></span><br><span class="line"><span class="comment">            Object object = it.next();</span></span><br><span class="line"><span class="comment">            if (object.equals(&quot;java2&quot;)) &#123;</span></span><br><span class="line"><span class="comment">                it.remove();//将java2的引用从集合中删除了</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">            sop(&quot;obj=&quot; + object);   //只是迭代器的元素被删除了，object还是正常输出</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        sop(arrayList);*/</span></span><br><span class="line">        sop(arrayList);</span><br><span class="line">        ListIterator lt = arrayList.listIterator();</span><br><span class="line">        <span class="comment">//sop(&quot;lt.hasPrevious():&quot; + lt.hasPrevious());</span></span><br><span class="line">        <span class="keyword">while</span> (lt.hasNext()) &#123;</span><br><span class="line">            Object obj = lt.next();</span><br><span class="line">            <span class="keyword">if</span> (obj.equals(<span class="string">&quot;java2&quot;</span>)) &#123;</span><br><span class="line">                <span class="comment">//lt.add(&quot;java520&quot;);</span></span><br><span class="line">                lt.set(<span class="string">&quot;java222&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//sop(&quot;lt.hasNext():&quot; + lt.hasNext());</span></span><br><span class="line">        <span class="comment">//sop(&quot;lt.hasPrevious():&quot; + lt.hasPrevious());</span></span><br><span class="line">        <span class="keyword">while</span> (lt.hasPrevious()) &#123;</span><br><span class="line">            sop(<span class="string">&quot;previous:&quot;</span> + lt.previous());<span class="comment">//反着取</span></span><br><span class="line">        &#125;</span><br><span class="line">        sop(arrayList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ArrayList arrayList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        arrayList.add(<span class="string">&quot;java1&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;java2&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;java3&quot;</span>);</span><br><span class="line">        sop(<span class="string">&quot;原集合是：&quot;</span> + arrayList);</span><br><span class="line">        <span class="comment">//在指定位置添加元素</span></span><br><span class="line">        arrayList.add(<span class="number">1</span>, <span class="string">&quot;xihuan&quot;</span>);</span><br><span class="line">        sop(<span class="string">&quot;添加元素后：&quot;</span> + arrayList);</span><br><span class="line">        <span class="comment">//删除指定位置的元素</span></span><br><span class="line">        arrayList.remove(<span class="number">1</span>);</span><br><span class="line">        sop(<span class="string">&quot;删除元素后：&quot;</span> + arrayList);</span><br><span class="line">        <span class="comment">//修改元素</span></span><br><span class="line">        arrayList.set(<span class="number">1</span>, <span class="string">&quot;我爱java&quot;</span>);</span><br><span class="line">        sop(<span class="string">&quot;修改元素后：|&quot;</span> + arrayList);</span><br><span class="line">        <span class="comment">//查找元素</span></span><br><span class="line">        sop(<span class="string">&quot;get(1):&quot;</span> + arrayList.get(<span class="number">1</span>));</span><br><span class="line">        sop(arrayList);</span><br><span class="line">        <span class="keyword">for</span> (ListIterator lt = arrayList.listIterator(); lt.hasNext(); ) &#123;</span><br><span class="line">            sop(<span class="string">&quot;next:&quot;</span> + lt.next());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//自定义输出方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sop</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        System.out.println(object);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>返回结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[java1, java2, java3]</span><br><span class="line">previous:java3</span><br><span class="line">previous:java222</span><br><span class="line">previous:java1</span><br><span class="line">[java1, java222, java3]</span><br></pre></td></tr></table></figure>

<h2 id="List的常用实现类"><a href="#List的常用实现类" class="headerlink" title="List的常用实现类"></a>List的常用实现类</h2><h3 id="ArrayList"><a href="#ArrayList" class="headerlink" title="ArrayList"></a>ArrayList</h3><p>底层是数组数据结构，特点是：<strong>查询快</strong>，<strong>增删慢</strong>，<strong>线程非同步</strong>，适用于单线程</p>
<p>去除ArrayList中重复元素</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 去除ArrayList中的重复元素</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayListTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ArrayList list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        list.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;python&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;python&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;python&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;C&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;C&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;C&quot;</span>);</span><br><span class="line">        ArrayList arrayList = singleElement(list);</span><br><span class="line">        System.out.println(arrayList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ArrayList <span class="title">singleElement</span><span class="params">(ArrayList arrayList)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//定义一个临时容器</span></span><br><span class="line">        ArrayList newArrayList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        Iterator it = arrayList.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            Object obj = it.next();</span><br><span class="line">            <span class="keyword">if</span> (!newArrayList.contains(obj)) &#123;</span><br><span class="line">                newArrayList.add(obj);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> newArrayList;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>最终返回结果只保存了不重复的元素</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[java, python, C]</span><br></pre></td></tr></table></figure>

<p>将自定义对象存储到 ArrayList 集合中，并去除重复元素</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将自定义对象存储到ArrayList集合中，并去除重复元素</span></span><br><span class="line"><span class="comment"> * 比如：存person对象，同名同龄视为同一个人，为重复元素</span></span><br><span class="line"><span class="comment"> * 思路：</span></span><br><span class="line"><span class="comment"> * 1、对人描述，将数据封装进人对象</span></span><br><span class="line"><span class="comment"> * 2、定义容器，将人存入</span></span><br><span class="line"><span class="comment"> * 3、取出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!(obj <span class="keyword">instanceof</span> Person)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Person person = (Person) obj;</span><br><span class="line">        System.out.println(<span class="keyword">this</span>.name + <span class="string">&quot;...&quot;</span> + person.name);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name.equals(person.name) &amp;&amp; <span class="keyword">this</span>.age == person.age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">(String name, Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayListTest2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sop</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        System.out.println(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ArrayList <span class="title">singleElement</span><span class="params">(ArrayList arrayList)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//定义一个临时容器</span></span><br><span class="line">        ArrayList newArrayList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        Iterator it = arrayList.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            Object obj = it.next();</span><br><span class="line">            <span class="keyword">if</span> (!newArrayList.contains(obj)) &#123;</span><br><span class="line">                newArrayList.add(obj);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> newArrayList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ArrayList arrayList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        arrayList.add(<span class="keyword">new</span> Person(<span class="string">&quot;zhangsan01&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        arrayList.add(<span class="keyword">new</span> Person(<span class="string">&quot;zhangsan02&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        arrayList.add(<span class="keyword">new</span> Person(<span class="string">&quot;zhangsan03&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        arrayList.add(<span class="keyword">new</span> Person(<span class="string">&quot;zhangsan04&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        <span class="comment">//ArrayList al = singleElement(arrayList);</span></span><br><span class="line">        sop(<span class="string">&quot;remove03:&quot;</span> + arrayList.remove(<span class="keyword">new</span> Person(<span class="string">&quot;zhangsan04&quot;</span>, <span class="number">20</span>)));<span class="comment">//底层是equals方法</span></span><br><span class="line">        Iterator it = arrayList.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            Person p = (Person) it.next();</span><br><span class="line">            sop(p.getName() + <span class="string">&quot;::&quot;</span> + p.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="LinkedList"><a href="#LinkedList" class="headerlink" title="LinkedList"></a>LinkedList</h3><p>底层是链表数据结构，特点是：<strong>查询慢</strong>，<strong>增删快</strong>，<strong>线程同步</strong></p>
<p>LinkedList特有方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">addFirst();</span><br><span class="line">addLast();</span><br><span class="line">getFirst();</span><br><span class="line">getLast();</span><br><span class="line"><span class="comment">//获取元素，但不删除元素，如果集合中没有元素，会出现NoSuchElementException</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">removeFirst();</span><br><span class="line">removeLast();</span><br><span class="line"><span class="comment">//获取元素，但元素被删除,如果集合中没有对应元素，会出现NoSuchElementException</span></span><br></pre></td></tr></table></figure>

<p>在 JDK1.6 出现了替代方法，</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">offerFirst();</span><br><span class="line">offerLast();</span><br><span class="line">peekFirst();</span><br><span class="line">peekLast();</span><br><span class="line"><span class="comment">//获取元素，但不删除元素，如果集合中没有元素，返回null</span></span><br><span class="line">pollFirst();</span><br><span class="line">pollLast();</span><br><span class="line"><span class="comment">//获取元素，但元素被删除,如果集合中没有元素，返回null</span></span><br></pre></td></tr></table></figure>

<p>从后往前取</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">huanhuan4</span><br><span class="line">huanhuan3</span><br><span class="line">huanhuan2</span><br><span class="line">huanhuan1</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.LinkedList;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用LinkedList模拟一个队列或者堆栈数据结构</span></span><br><span class="line"><span class="comment"> * 堆栈：先进后出  如同一个杯子</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 队列：先进先出  如同一根水管  FIFO(First In First Out)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 队列:先进先出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Queue</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> LinkedList linkedList;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Queue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        linkedList = <span class="keyword">new</span> LinkedList();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">myAdd</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        linkedList.addFirst(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">myGet</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> linkedList.removeLast();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isNull</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> linkedList.isEmpty();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 堆栈:后进先出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stack</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> LinkedList linkedList;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Stack</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        linkedList = <span class="keyword">new</span> LinkedList();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">myAdd</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        linkedList.addFirst(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">myGet</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> linkedList.removeFirst();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isNull</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> linkedList.isEmpty();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkedListTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Queue queue = <span class="keyword">new</span> Queue();</span><br><span class="line">        queue.myAdd(<span class="string">&quot;java01&quot;</span>);</span><br><span class="line">        queue.myAdd(<span class="string">&quot;java02&quot;</span>);</span><br><span class="line">        queue.myAdd(<span class="string">&quot;java03&quot;</span>);</span><br><span class="line">        queue.myAdd(<span class="string">&quot;java04&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        System.out.println(queue.myGet());</span></span><br><span class="line">        <span class="keyword">while</span> (!queue.isNull()) &#123;</span><br><span class="line">            System.out.println(queue.myGet());</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;========&quot;</span>);</span><br><span class="line">        Stack stack = <span class="keyword">new</span> Stack();</span><br><span class="line">        stack.myAdd(<span class="string">&quot;java01&quot;</span>);</span><br><span class="line">        stack.myAdd(<span class="string">&quot;java02&quot;</span>);</span><br><span class="line">        stack.myAdd(<span class="string">&quot;java03&quot;</span>);</span><br><span class="line">        stack.myAdd(<span class="string">&quot;java04&quot;</span>);</span><br><span class="line">        <span class="keyword">while</span> (!stack.isNull()) &#123;</span><br><span class="line">            System.out.println(stack.myGet());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">java01</span><br><span class="line">java02</span><br><span class="line">java03</span><br><span class="line">java04</span><br><span class="line">&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;</span><br><span class="line">java04</span><br><span class="line">java03</span><br><span class="line">java02</span><br><span class="line">java01</span><br></pre></td></tr></table></figure>

<h3 id="Vector"><a href="#Vector" class="headerlink" title="Vector"></a>Vector</h3><p>底层是数组数据结构，特点是：查询慢，增删也慢，线程同步</p>
<p>Vector取出元素的方式：枚举</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Enumeration;</span><br><span class="line"><span class="keyword">import</span> java.util.Vector;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 枚举就是Vector特有的取出方式</span></span><br><span class="line"><span class="comment"> * 枚举和迭代器很相似</span></span><br><span class="line"><span class="comment"> * 其实枚举和迭代是一样的</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 因为枚举的名称以及方法的名称都过长，所以被迭代器取代了</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">VectorDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sop</span><span class="params">(Object object)</span></span>&#123;</span><br><span class="line">        System.out.println(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Vector vector = <span class="keyword">new</span> Vector();</span><br><span class="line">        vector.add(<span class="string">&quot;java01&quot;</span>);</span><br><span class="line">        vector.add(<span class="string">&quot;java02&quot;</span>);</span><br><span class="line">        vector.add(<span class="string">&quot;java03&quot;</span>);</span><br><span class="line">        vector.add(<span class="string">&quot;java04&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Enumeration elements = vector.elements();</span><br><span class="line">        <span class="keyword">while</span> (elements.hasMoreElements())&#123;</span><br><span class="line">            sop(elements.nextElement());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="Set"><a href="#Set" class="headerlink" title="Set"></a>Set</h2><p>Set的元素是无序的(存入和取出的顺序不一定一致)，并且不重复</p>
<h2 id="Set的常用实现类"><a href="#Set的常用实现类" class="headerlink" title="Set的常用实现类"></a>Set的常用实现类</h2><h3 id="HashSet"><a href="#HashSet" class="headerlink" title="HashSet"></a>HashSet</h3><p>底层是哈希表，线程非同步</p>
<p>HashSet是如何保证元素的唯一性呢？</p>
<p>通过元素对象的两个方法，hashCode()和equals()来完成</p>
<p>如果元素的哈希值相同，才会调用equals方法来判断对象是否相等</p>
<p>如果元素的哈希值不同，就不会调用equals方法</p>
<p>记住这个结论：哈希值相等，两个对象并不一定相等；反之，equals方法返回true，那么这两个对象一定相等。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashSet;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * |--Set：元素是无序的(存入和取出的顺序不一定一致)，元素不重复</span></span><br><span class="line"><span class="comment"> * |--HashSet:底层是哈希表</span></span><br><span class="line"><span class="comment"> *          HashSet是如何保证元素唯一性？</span></span><br><span class="line"><span class="comment"> *          通过元素的两个方法，hashCode()和equals()来完成</span></span><br><span class="line"><span class="comment"> *          通过元素的哈希值相同，才会判断equals是否为true</span></span><br><span class="line"><span class="comment"> *          如果元素的哈希值不同，就不会调用equals</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *          注意：对于判断元素是否存在以及删除等操作，都依赖的方法是元素的hashCode和equals方法</span></span><br><span class="line"><span class="comment"> * |--TreeSet:</span></span><br><span class="line"><span class="comment"> * Set集合与Collection的功能是一致的</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HashSetDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sop</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        System.out.println(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        HashSet hashSet = <span class="keyword">new</span> HashSet();</span><br><span class="line">        hashSet.add(<span class="string">&quot;java01&quot;</span>);</span><br><span class="line">        hashSet.add(<span class="string">&quot;java02&quot;</span>);</span><br><span class="line">        hashSet.add(<span class="string">&quot;java03&quot;</span>);</span><br><span class="line">        hashSet.add(<span class="string">&quot;java03&quot;</span>);</span><br><span class="line">        hashSet.add(<span class="string">&quot;java04&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Iterator it = hashSet.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext())&#123;</span><br><span class="line">            sop(it.next());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>重复元素不能存入HashSet</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">java04</span><br><span class="line">java03</span><br><span class="line">java02</span><br><span class="line">java01</span><br></pre></td></tr></table></figure>

<p>姓名与年龄相同为同一条小狗，认为是重复元素，不存入HashSet</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashSet;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 往HashSet中存入自定义对象</span></span><br><span class="line"><span class="comment"> * 姓名与年龄相同为同一条小狗，认为是重复元素，不存入HashSet</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//重写hashCode()方法，返回一个固定的哈希值</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">60</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//重写equals(Object obj)方法，定义所需要的变量比较</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//判断传入的Obj是否是由Dog下转型的变量</span></span><br><span class="line">        <span class="keyword">if</span> (!(obj <span class="keyword">instanceof</span> Dog)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//对传入的对象强制转换</span></span><br><span class="line">        Dog dog = (Dog) obj;</span><br><span class="line">        System.out.println(<span class="keyword">this</span>.name + <span class="string">&quot;...&quot;</span> + dog.name);</span><br><span class="line">        <span class="comment">//判断两个对象是否名字和年龄相等，相等则认为是同一个对象</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name.equals(dog.name) &amp;&amp; <span class="keyword">this</span>.age == dog.age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(String name, Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HashSetTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sop</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">        System.out.println(object);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        HashSet hashSet = <span class="keyword">new</span> HashSet();</span><br><span class="line">        hashSet.add(<span class="keyword">new</span> Dog(<span class="string">&quot;小白&quot;</span>, <span class="number">3</span>));</span><br><span class="line">        hashSet.add(<span class="keyword">new</span> Dog(<span class="string">&quot;小白&quot;</span>, <span class="number">3</span>));</span><br><span class="line">        hashSet.add(<span class="keyword">new</span> Dog(<span class="string">&quot;小黄&quot;</span>, <span class="number">4</span>));</span><br><span class="line">        hashSet.add(<span class="keyword">new</span> Dog(<span class="string">&quot;小黄&quot;</span>, <span class="number">4</span>));</span><br><span class="line">        hashSet.add(<span class="keyword">new</span> Dog(<span class="string">&quot;小黄&quot;</span>, <span class="number">5</span>));</span><br><span class="line"></span><br><span class="line">        Iterator iterator = hashSet.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            Dog dog = (Dog) iterator.next();</span><br><span class="line">            sop(dog.getName() + <span class="string">&quot;:&quot;</span> + dog.getAge());<span class="comment">//输出结果是无序的</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">小白...小白</span><br><span class="line">小黄...小白</span><br><span class="line">小黄...小白</span><br><span class="line">小黄...小黄</span><br><span class="line">小黄...小白</span><br><span class="line">小黄...小黄</span><br><span class="line">小白:3</span><br><span class="line">小黄:4</span><br><span class="line">小黄:5</span><br></pre></td></tr></table></figure>

<h3 id="TreeSet"><a href="#TreeSet" class="headerlink" title="TreeSet"></a>TreeSet</h3><p>底层是二叉树</p>
<p>TreeSet保证元素唯一性的方式有两种：</p>
<p>第一种：让元素自身具备比较性，元素对象的类实现Comparable 接口并覆盖compareTo方法，定义比较规则，这种方式也称为元素的自然顺序（或者默认顺序）</p>
<p>第二种：当元素自身不具备比较性，或者说元素具备的比较性不是我们所需要的，</p>
<p>这时需要让集合自身具备比较性，自定义比较器。</p>
<p>如何自定义一个比较器呢？</p>
<p>定义一个类，实现Comparator接口，覆盖compare方法，具体可参考下面的代码示例</p>
<p>在集合声明初始化时将自定义比较器放入集合</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">TreeSet treeSet = <span class="keyword">new</span> TreeSet(<span class="keyword">new</span> MyCompare());<span class="comment">//new MyCompare()是自定义的比较器</span></span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"><span class="keyword">import</span> java.util.TreeSet;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 当两种排序规则都存在时，以比较器为主</span></span><br><span class="line"><span class="comment"> *  一、定义一个类，实现Comparator接口，覆盖compare方法  1元素大，0元素相同，-1元素小</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 需求：往treeSet集合中存储自定义对象</span></span><br><span class="line"><span class="comment"> * 想按照学生的年龄进行排序</span></span><br><span class="line"><span class="comment"> * java.lang.ClassCastException: Collection.Student cannot be cast to java.lang.Comparable</span></span><br><span class="line"><span class="comment"> * 排序时，当主要条件相同时，一定要判断次要条件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyCompare</span> <span class="keyword">implements</span> <span class="title">Comparator</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(Object o1, Object o2)</span> </span>&#123;</span><br><span class="line">        Student student1 = (Student) o1;</span><br><span class="line">        Student student2 = (Student) o2;</span><br><span class="line">        <span class="keyword">int</span> i = student1.getName().compareTo(student2.getName());</span><br><span class="line">        <span class="keyword">if</span> (i == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">/*if (student1.getAge() &gt; student2.getAge())</span></span><br><span class="line"><span class="comment">                return 1;</span></span><br><span class="line"><span class="comment">            if (student1.getAge() == student2.getAge())</span></span><br><span class="line"><span class="comment">                return 0;</span></span><br><span class="line"><span class="comment">            if (student1.getAge() &lt; student2.getAge())</span></span><br><span class="line"><span class="comment">                return -1;*/</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> Integer(student1.getAge()).compareTo(student2.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TreeSetTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//自定义比较器对象放入TreeSet的构造函数</span></span><br><span class="line">        TreeSet treeSet = <span class="keyword">new</span> TreeSet(<span class="keyword">new</span> MyCompare());</span><br><span class="line">        <span class="comment">//通过实现Comparable接口并重写compareTo方法实现通过年龄大小对对象进行排序</span></span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;张三001&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;张三02&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;张三02&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;李四07&quot;</span>, <span class="number">19</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;李四07&quot;</span>, <span class="number">19</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;王五03&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Student(<span class="string">&quot;王五03&quot;</span>, <span class="number">2</span>));</span><br><span class="line"></span><br><span class="line">        Iterator iterator = treeSet.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            <span class="comment">//输出结果是排序的</span></span><br><span class="line">            Student student = (Student) iterator.next();</span><br><span class="line">            System.out.println(student.getName() + <span class="string">&quot;...&quot;</span> + student.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>重复元素没有存入TreeSet</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">张三001...18</span><br><span class="line">张三02...18</span><br><span class="line">李四07...19</span><br><span class="line">王五03...2</span><br><span class="line">王五03...20</span><br></pre></td></tr></table></figure>

<h2 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h2><p>我感觉泛型这玩意确实挺有意思的，不得不佩服这些设计语言的大佬们！</p>
<p>它是jdk 1.5的新特性，用于解决安全问题，是一个元素类型的安全机制</p>
<p>好处：</p>
<ul>
<li>将运行时出现的问题ClassCastException，转移到编译前，事实上，现在的idea中会直接报错，连编译不会让你编译，先把错误解决了再编译，有利于开发…</li>
<li>避免了强制类型转换的麻烦</li>
</ul>
<p>泛型的格式：’&lt;&gt;’来定义要操作的引用数据类型</p>
<p>在使用Java提供的对象时，什么时候使用泛型呢？</p>
<p>通常在集合框架中十分常见。</p>
<p>一个简单的泛型Demo</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 泛型:  jdk1.5版本的新特性，用于解决安全问题，是一个元素类型安全机制</span></span><br><span class="line"><span class="comment"> * 好处：一、将运行时出现的问题ClassCastException,转移到了编译时期</span></span><br><span class="line"><span class="comment"> * 方便于开发人员解决问题，让运行时的问题减少，代码更严谨</span></span><br><span class="line"><span class="comment"> * 二、避免了强制转换的麻烦</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 泛型格式： &lt;&gt;来定义要操作的引用数据类型</span></span><br><span class="line"><span class="comment"> * 在使用Java提供的对象时，什么时候使用泛型呢？</span></span><br><span class="line"><span class="comment"> * 通常在集合框架中很常见</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//&lt;String&gt;就是泛型，指定元素类型，我定义了一个ArrayList容器，这个容器的元素是String类型</span></span><br><span class="line">        ArrayList&lt;String&gt; arrayList = <span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line">        arrayList.add(<span class="string">&quot;abc01&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;abc02&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;abc03&quot;</span>);</span><br><span class="line">        arrayList.add(<span class="string">&quot;abc04&quot;</span>);</span><br><span class="line">        <span class="comment">//arrayList.add(4);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//指定迭代器装的元素类型也是String，使用这种操作更安全</span></span><br><span class="line">        Iterator&lt;String&gt; iterator = arrayList.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            String s = iterator.next();</span><br><span class="line">            System.out.println(<span class="string">&quot;string:&quot;</span> + s + <span class="string">&quot;,length:&quot;</span> + s.length());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string:abc01,length:5</span><br><span class="line">string:abc02,length:5</span><br><span class="line">string:abc03,length:5</span><br><span class="line">string:abc04,length:5</span><br></pre></td></tr></table></figure>

<h3 id="比较器与泛型"><a href="#比较器与泛型" class="headerlink" title="比较器与泛型"></a>比较器与泛型</h3><p>泛型的应用很多，在自定义比较器中也可以指定泛型</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"><span class="keyword">import</span> java.util.TreeSet;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 泛型demo2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        TreeSet&lt;String&gt; treeSet = <span class="keyword">new</span> TreeSet&lt;String&gt;(<span class="keyword">new</span> StrLenComparator());</span><br><span class="line">        treeSet.add(<span class="string">&quot;huanhuan&quot;</span>);</span><br><span class="line">        treeSet.add(<span class="string">&quot;xixi&quot;</span>);</span><br><span class="line">        treeSet.add(<span class="string">&quot;woshuo&quot;</span>);</span><br><span class="line">        treeSet.add(<span class="string">&quot;ipotatoyou&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Iterator&lt;String&gt; iterator = treeSet.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            String s = iterator.next();</span><br><span class="line">            System.out.println(s + <span class="string">&quot;---&quot;</span> + s.length());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义一个比较器,比较器也可以指定泛型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StrLenComparator</span> <span class="keyword">implements</span> <span class="title">Comparator</span>&lt;<span class="title">String</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(String o1, String o2)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//比较长度</span></span><br><span class="line">        <span class="keyword">int</span> result = <span class="keyword">new</span> Integer(o2.length()).compareTo(<span class="keyword">new</span> Integer(o1.length()));<span class="comment">//想按字符串长度升降序排序很简单，倒序之间交换参数o1、o2位置即可</span></span><br><span class="line">        <span class="keyword">if</span> (result == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> o2.compareTo(o1);<span class="comment">//长度一样则按默认比较性</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">xixi---4</span><br><span class="line">woshuo---6</span><br><span class="line">huanhuan---8</span><br><span class="line">ipotatoyou---10</span><br></pre></td></tr></table></figure>

<h3 id="泛型类"><a href="#泛型类" class="headerlink" title="泛型类"></a>泛型类</h3><p>还可以定义泛型类，那什么时候需要定义泛型类呢？</p>
<p>当类中要操作的引用数据类型不确定的时候，早期定义Object来完成扩展，现在定义泛型类来完成扩展</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义泛型类</span></span><br><span class="line"><span class="comment"> * 什么时候需要定义泛型类？</span></span><br><span class="line"><span class="comment"> * 当类中要操作的引用数据类型不确定的时候</span></span><br><span class="line"><span class="comment"> * 早期定义Object来完成扩展，现在定义泛型类来完成扩展</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">/*class Tool&#123;</span></span><br><span class="line"><span class="comment">    private Worker worker;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    public Worker getWorker() &#123;</span></span><br><span class="line"><span class="comment">        return worker;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    public void setWorker(Worker worker) &#123;</span></span><br><span class="line"><span class="comment">        this.worker = worker;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;*/</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Worker</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 早期的处理方式，但是需要强制类型转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tool</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Object obj;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">getObj</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setObj</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.obj = obj;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 传说中的泛型类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;QQ&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Utils</span>&lt;<span class="title">QQ</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> QQ qq;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setObject</span><span class="params">(QQ qq)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.qq = qq;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> QQ <span class="title">getObject</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> qq;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericDemo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Utils&lt;Worker&gt; utils = <span class="keyword">new</span> Utils&lt;&gt;();</span><br><span class="line">        utils.setObject(<span class="keyword">new</span> Worker());</span><br><span class="line">        Worker w = utils.getObject();</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*Tool t = new Tool();</span></span><br><span class="line"><span class="comment">        t.setObj(new Student1());</span></span><br><span class="line"><span class="comment">        Worker w = (Worker) t.getObj();*/</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="泛型限定"><a href="#泛型限定" class="headerlink" title="泛型限定"></a>泛型限定</h3><p>语法：&lt;? extends Animal&gt;     ? 通配符，可以理解为占位符</p>
<p>泛型的限定：</p>
<? extends E>     可以接收E类型或者E的子类型，这是向上的限定

<? super E>        可以接收E类型或者E的父类型，这是向下限定

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 泛型限定</span></span><br><span class="line"><span class="comment"> * &lt;? extends Animal&gt;   ？ 通配符，可以理解为占位符</span></span><br><span class="line"><span class="comment"> *泛型的限定：</span></span><br><span class="line"><span class="comment"> * &lt;? extends E&gt;   可以接收E类型或者E的子类型，这是向上限定</span></span><br><span class="line"><span class="comment"> * &lt;? super E&gt;      可以接收E类型或者E的父类型，向下限定</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericDemo4</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*ArrayList&lt;String&gt; arrayList1 = new ArrayList&lt;&gt;();</span></span><br><span class="line"><span class="comment">        arrayList1.add(&quot;abc&quot;);</span></span><br><span class="line"><span class="comment">        arrayList1.add(&quot;abc520&quot;);</span></span><br><span class="line"><span class="comment">        arrayList1.add(&quot;abc666&quot;);</span></span><br><span class="line"><span class="comment">        ArrayList&lt;Integer&gt; arrayList2 = new ArrayList&lt;&gt;();</span></span><br><span class="line"><span class="comment">        arrayList2.add(1);</span></span><br><span class="line"><span class="comment">        arrayList2.add(3);</span></span><br><span class="line"><span class="comment">        arrayList2.add(1);</span></span><br><span class="line"><span class="comment">        arrayList2.add(4);</span></span><br><span class="line"><span class="comment">        printColl(arrayList1);</span></span><br><span class="line"><span class="comment">        printColl(arrayList2);*/</span></span><br><span class="line">        ArrayList&lt;Animal&gt; arrayList1 = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        arrayList1.add(<span class="keyword">new</span> Animal(<span class="string">&quot;小猫&quot;</span>));</span><br><span class="line">        arrayList1.add(<span class="keyword">new</span> Animal(<span class="string">&quot;小狗&quot;</span>));</span><br><span class="line">        arrayList1.add(<span class="keyword">new</span> Animal(<span class="string">&quot;小猪&quot;</span>));</span><br><span class="line">        printColl(arrayList1);</span><br><span class="line"></span><br><span class="line">        ArrayList&lt;Cat&gt; arrayList2 = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        arrayList2.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小花猫&quot;</span>));</span><br><span class="line">        arrayList2.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小白猫&quot;</span>));</span><br><span class="line">        arrayList2.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小狸猫&quot;</span>));</span><br><span class="line">        printColl(arrayList2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">printColl</span><span class="params">(ArrayList&lt;? extends Animal&gt; arrayList)</span> </span>&#123;   <span class="comment">//&lt;? extends Animal&gt;    泛型限定，只能存放Animal和其子类对象</span></span><br><span class="line">        Iterator&lt;? extends Animal&gt; iterator = arrayList.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            System.out.println(iterator.next().getName());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">   <span class="comment">/* public static void printColl(ArrayList&lt;?&gt; arrayList) &#123;//?   占位符，不明确是什么类型</span></span><br><span class="line"><span class="comment">        Iterator&lt;?&gt; iterator = arrayList.iterator();</span></span><br><span class="line"><span class="comment">        while (iterator.hasNext())&#123;</span></span><br><span class="line"><span class="comment">            System.out.println(iterator.next());</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;*/</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Animal</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Cat</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

向上限定，既可以输出Animal对象，也可以输出继承Animal的Cat对象，非常有意思...

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">小猫</span><br><span class="line">小狗</span><br><span class="line">小猪</span><br><span class="line">小花猫</span><br><span class="line">小白猫</span><br><span class="line">小狸猫</span><br></pre></td></tr></table></figure>

通过这个特性，我们可以定义父类的比较器，泛型定义为父类，这样子类可以使用这个比较器来指定排序规则

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"><span class="keyword">import</span> java.util.TreeSet;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericDemo5</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        TreeSet&lt;Cat&gt; treeSet = <span class="keyword">new</span> TreeSet&lt;&gt;(<span class="keyword">new</span> CompareAnimal());</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小猫01&quot;</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小猫06&quot;</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小猫03&quot;</span>));</span><br><span class="line">        treeSet.add(<span class="keyword">new</span> Cat(<span class="string">&quot;小猫07&quot;</span>));</span><br><span class="line"></span><br><span class="line">        Iterator&lt;Cat&gt; iterator = treeSet.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            System.out.println(iterator.next().getName());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//折耳猫 extends Cat</span></span><br><span class="line">        TreeSet&lt;ZheErCat&gt; treeSet1 = <span class="keyword">new</span> TreeSet&lt;&gt;(<span class="keyword">new</span> CompareAnimal());</span><br><span class="line">        treeSet1.add(<span class="keyword">new</span> ZheErCat(<span class="string">&quot;折耳猫01&quot;</span>));</span><br><span class="line">        treeSet1.add(<span class="keyword">new</span> ZheErCat(<span class="string">&quot;折耳猫06&quot;</span>));</span><br><span class="line">        treeSet1.add(<span class="keyword">new</span> ZheErCat(<span class="string">&quot;折耳猫03&quot;</span>));</span><br><span class="line">        treeSet1.add(<span class="keyword">new</span> ZheErCat(<span class="string">&quot;折耳猫07&quot;</span>));</span><br><span class="line"></span><br><span class="line">        Iterator&lt;ZheErCat&gt; iterator1 = treeSet1.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator1.hasNext()) &#123;</span><br><span class="line">            System.out.println(iterator1.next().getName());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 指定父类型Animal的比较器</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CompareAnimal</span> <span class="keyword">implements</span> <span class="title">Comparator</span>&lt;<span class="title">Animal</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(Animal o1, Animal o2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> result = o2.getName().compareTo(o1.getName());<span class="comment">//DESC</span></span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ZheErCat</span> <span class="keyword">extends</span> <span class="title">Cat</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ZheErCat</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义猫的比较器 Cat extends Animal</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">class CompareCat implements Comparator&lt;Cat&gt; &#123;</span></span><br><span class="line"><span class="comment">    @Override</span></span><br><span class="line"><span class="comment">    public int compare(Cat o1, Cat o2) &#123;</span></span><br><span class="line"><span class="comment">        int result = o1.getName().compareTo(o2.getName());</span></span><br><span class="line"><span class="comment">        return result;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义折耳猫的比较器 ZheErCat extends Cat</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">class CompareZheErCat implements Comparator&lt;ZheErCat&gt; &#123;</span></span><br><span class="line"><span class="comment">    @Override</span></span><br><span class="line"><span class="comment">    public int compare(ZheErCat o1, ZheErCat o2) &#123;</span></span><br><span class="line"><span class="comment">        return o1.getName().compareTo(o2.getName());</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

倒序输出

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">小猫07</span><br><span class="line">小猫06</span><br><span class="line">小猫03</span><br><span class="line">小猫01</span><br><span class="line">折耳猫07</span><br><span class="line">折耳猫06</span><br><span class="line">折耳猫03</span><br><span class="line">折耳猫01</span><br></pre></td></tr></table></figure>

### 泛型与接口

将泛型定义在接口上

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Collection;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 泛型定义在接口上</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Generic</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*class GenericImpl implements Generic&lt;String&gt; &#123;</span></span><br><span class="line"><span class="comment">    @Override</span></span><br><span class="line"><span class="comment">    public void show(String s) &#123;</span></span><br><span class="line"><span class="comment">        System.out.println(&quot;show method:&quot; + s);</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GenericImpl</span>&lt;<span class="title">T</span>&gt; <span class="keyword">implements</span> <span class="title">Generic</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;t:&quot;</span> + t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericInterface</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        GenericImpl generic = <span class="keyword">new</span> GenericImpl();</span><br><span class="line">        generic.show(<span class="number">666</span>);</span><br><span class="line">        generic.show(<span class="string">&quot;what can I do for you?&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">t:666</span><br><span class="line">t:what can I do for you?</span><br></pre></td></tr></table></figure>

### 泛型类中定义泛型方法

泛型类中定义的泛型，在整个类中有效，如果被方法调用

那么泛型类的对象明确要操作的类型后，所有要操作的类型就已经固定了



为了让不同方法可以操作不同类型，而且类型还不确定，可以将泛型定义在方法上

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;show:&quot;</span> + t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;print:&quot;</span> + t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

泛型类中可以同时存在泛型方法

**特殊之处**：静态方法不可以访问类上定义的泛型

如果静态方法操作的引用数据类型不确定，可以将泛型定义在方法上

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;show:&quot;</span> + t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> &lt;Q&gt; <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">(Q q)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;print:&quot;</span> + q);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 静态泛型方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> w</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> &lt;W&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> &lt;W&gt; <span class="function"><span class="keyword">void</span> <span class="title">method</span><span class="params">(W w)</span> </span>&#123;    <span class="comment">//建立对象的时候这个T才会被指定它的引用类型</span></span><br><span class="line">        System.out.println(<span class="string">&quot;static generic method:&quot;</span> + w);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericMethod</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Demo&lt;String&gt; demo = <span class="keyword">new</span> Demo&lt;&gt;();</span><br><span class="line">        demo.show(<span class="string">&quot;haha&quot;</span>);</span><br><span class="line"><span class="comment">//        demo.show(666);</span></span><br><span class="line">        demo.print(<span class="number">500</span>);</span><br><span class="line">        demo.print(<span class="string">&quot;泛型方法&quot;</span>);</span><br><span class="line">        Demo.method(<span class="string">&quot;I POTATO YOU&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

输出

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">show:haha</span><br><span class="line">print:500</span><br><span class="line">print:泛型方法</span><br><span class="line">static generic method:I POTATO YOU</span><br></pre></td></tr></table></figure>

        </div>
        <footer class="article-footer">
            



    <a data-url="https://hchdream.gitee.io/hexo/2020/05/14/Collection%E9%9B%86%E5%90%88%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E5%8F%8A%E4%BB%A3%E7%A0%81/" data-id="ckqnkn7jg0020s8rj68tl5abd" class="article-share-link"><i class="fa fa-share"></i>分享到</a>
<script>
    (function ($) {
        $('body').on('click', function() {
            $('.article-share-box.on').removeClass('on');
        }).on('click', '.article-share-link', function(e) {
            e.stopPropagation();

            var $this = $(this),
                url = $this.attr('data-url'),
                encodedUrl = encodeURIComponent(url),
                id = 'article-share-box-' + $this.attr('data-id'),
                offset = $this.offset(),
                box;

            if ($('#' + id).length) {
                box = $('#' + id);

                if (box.hasClass('on')){
                    box.removeClass('on');
                    return;
                }
            } else {
                var html = [
                    '<div id="' + id + '" class="article-share-box">',
                        '<input class="article-share-input" value="' + url + '">',
                        '<div class="article-share-links">',
                            '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
                            '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
                            '<a href="http://pinterest.com/pin/create/button/?url=' + encodedUrl + '" class="article-share-pinterest" target="_blank" title="Pinterest"></a>',
                            '<a href="https://plus.google.com/share?url=' + encodedUrl + '" class="article-share-google" target="_blank" title="Google+"></a>',
                        '</div>',
                    '</div>'
                ].join('');

              box = $(html);

              $('body').append(box);
            }

            $('.article-share-box.on').hide();

            box.css({
                top: offset.top + 25,
                left: offset.left
            }).addClass('on');

        }).on('click', '.article-share-box', function (e) {
            e.stopPropagation();
        }).on('click', '.article-share-box-input', function () {
            $(this).select();
        }).on('click', '.article-share-box-link', function (e) {
            e.preventDefault();
            e.stopPropagation();

            window.open(this.href, 'article-share-box-window-' + Date.now(), 'width=500,height=450');
        });
    })(jQuery);
</script>

        </footer>
    </div>
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "BlogPosting",
        "author": {
            "@type": "Person",
            "name": "XiaoHuang"
        },
        "headline": "Java之Collection学习笔记",
        "image": "https://hchdream.gitee.io/hexohttps://hchdream.gitee.io/images/img/portfolio/04-large.jpg",
        "keywords": "Collection List Set 泛型",
        "genre": "Java",
        "datePublished": "2020-05-14",
        "dateCreated": "2020-05-14",
        "dateModified": "2020-10-28",
        "url": "https://hchdream.gitee.io/hexo/2020/05/14/Collection集合学习笔记及代码/",
        "description": "
CollectionInterface Collection 来自java.util包下，是非常实用的数据结构，字面意思是容器。
JDK中的描述：它是集合层次结构根接口。一个集合代表一组对象，被称为元素。一些集合允许重复的元素，而其他的则不允许。有些是有序和无序的人。JDK没有提供这个接口的任何直接的实现：它提供更具体的子接口像  Set和 List实现。此接口通常用于通过收集和操纵他们最大的共",
        "wordCount": 7839
    }
</script>

</article>

    <section id="comments">
    
        
    <div id="disqus_thread">
        <noscript>Please enable JavaScript to view the <a target="_blank" rel="noopener" href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    </div>


    
    </section>



                        </div>
                    </section>
                    <aside id="sidebar">
    <a class="sidebar-toggle" title="Expand Sidebar"><i class="toggle icon"></i></a>
    <div class="sidebar-top">
        <p>关注我 :</p>
        <ul class="social-links">
            
                
                <li>
                    <a class="social-tooltip" title="github" href="https://github.com/hchdream" target="_blank" rel="noopener">
                        <i class="icon fa fa-github"></i>
                    </a>
                </li>
                
            
                
                <li>
                    <a class="social-tooltip" title="rss" href="/hexo/atom.xml" target="_blank" rel="noopener">
                        <i class="icon fa fa-rss"></i>
                    </a>
                </li>
                
            
        </ul>
    </div>
    
        
<nav id="article-nav">
    
        <a href="/hexo/2020/05/16/%E5%8D%95%E4%BE%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption">下一篇</strong>
        <p class="article-nav-title">
        
            单例设计模式
        
        </p>
        <i class="icon fa fa-chevron-right" id="icon-chevron-right"></i>
    </a>
    
    
        <a href="/hexo/2020/05/02/mybatis%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/" id="article-nav-older" class="article-nav-link-wrap">
        <strong class="article-nav-caption">上一篇</strong>
        <p class="article-nav-title">mybaits的配置文件</p>
        <i class="icon fa fa-chevron-left" id="icon-chevron-left"></i>
        </a>
    
</nav>

    
    <div class="widgets-container">
        
            
                

            
                
    <div class="widget-wrap">
        <h3 class="widget-title">最新文章</h3>
        <div class="widget">
            <ul id="recent-post" class="">
                
                    <li>
                        
                        <div class="item-thumbnail">
                            <a href="/hexo/2021/07/03/SpringCloud-Alibaba/" class="thumbnail">
    
    
        <span style="background-image:url(https://w.wallhaven.cc/full/j3/wallhaven-j3wqwm.jpg)" alt="SpringCloud&amp;Alibaba" class="thumbnail-image"></span>
    
    
</a>

                        </div>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/hexo/categories/springcloud/">springcloud</a></p>
                            <p class="item-title"><a href="/hexo/2021/07/03/SpringCloud-Alibaba/" class="title">SpringCloud&amp;Alibaba</a></p>
                            <p class="item-date"><time datetime="2021-07-03T08:26:10.000Z" itemprop="datePublished">2021-07-03</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-thumbnail">
                            <a href="/hexo/2021/01/29/nginx/" class="thumbnail">
    
    
        <span style="background-image:url(http://hchdream.gitee.io/images/img/portfolio/08-large.jpg)" alt="nginx入门" class="thumbnail-image"></span>
    
    
</a>

                        </div>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/hexo/categories/nginx/">nginx</a></p>
                            <p class="item-title"><a href="/hexo/2021/01/29/nginx/" class="title">nginx入门</a></p>
                            <p class="item-date"><time datetime="2021-01-28T16:00:00.000Z" itemprop="datePublished">2021-01-29</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-thumbnail">
                            <a href="/hexo/2020/10/29/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" class="thumbnail">
    
    
        <span style="background-image:url(http://hchdream.gitee.io/images/img/portfolio/10-large.jpg)" alt="网络编程" class="thumbnail-image"></span>
    
    
</a>

                        </div>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/hexo/categories/Java/">Java</a></p>
                            <p class="item-title"><a href="/hexo/2020/10/29/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" class="title">网络编程</a></p>
                            <p class="item-date"><time datetime="2020-10-29T05:39:12.000Z" itemprop="datePublished">2020-10-29</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-thumbnail">
                            <a href="/hexo/2020/10/27/%E6%B5%8B%E8%AF%95/" class="thumbnail">
    
    
        <span style="background-image:url(https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2926199504,3558053336&fm=26&gp=0.jpg)" alt="测试" class="thumbnail-image"></span>
    
    
</a>

                        </div>
                        
                        <div class="item-inner">
                            <p class="item-category"></p>
                            <p class="item-title"><a href="/hexo/2020/10/27/%E6%B5%8B%E8%AF%95/" class="title">测试</a></p>
                            <p class="item-date"><time datetime="2020-10-27T08:16:08.000Z" itemprop="datePublished">2020-10-27</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-thumbnail">
                            <a href="/hexo/2020/10/27/hello-world/" class="thumbnail">
    
    
        <span style="background-image:url(https://hchdream.gitee.io/images/img/portfolio/04-large.jpg)" alt="Hello World" class="thumbnail-image"></span>
    
    
</a>

                        </div>
                        
                        <div class="item-inner">
                            <p class="item-category"></p>
                            <p class="item-title"><a href="/hexo/2020/10/27/hello-world/" class="title">Hello World</a></p>
                            <p class="item-date"><time datetime="2020-10-27T05:33:45.370Z" itemprop="datePublished">2020-10-27</time></p>
                        </div>
                    </li>
                
            </ul>
        </div>
    </div>

            
                
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">分类</h3>
        <div class="widget">
            <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/Java/">Java</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/Spring/">Spring</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/nginx/">nginx</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/springcloud/">springcloud</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F/">镜像加速</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/hexo/categories/%E9%A1%B9%E7%9B%AE/">项目</a><span class="category-list-count">1</span></li></ul>
        </div>
    </div>


            
                
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">归档</h3>
        <div class="widget">
            <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/07/">七月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">一月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2020/10/">十月 2020</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2020/06/">六月 2020</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2020/05/">五月 2020</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2020/04/">四月 2020</a><span class="archive-list-count">3</span></li></ul>
        </div>
    </div>


            
                
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">标签</h3>
        <div class="widget">
            <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/AOP/" rel="tag">AOP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/Collection/" rel="tag">Collection</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/DI/" rel="tag">DI</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/HashMap/" rel="tag">HashMap</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/HashTabel/" rel="tag">HashTabel</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/IOC/" rel="tag">IOC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/List/" rel="tag">List</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/Map/" rel="tag">Map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/Set/" rel="tag">Set</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/TreeMap/" rel="tag">TreeMap</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/idea/" rel="tag">idea</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/jdbc/" rel="tag">jdbc</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/jpa/" rel="tag">jpa</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/mybatis/" rel="tag">mybatis</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/mysql/" rel="tag">mysql</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/npm/" rel="tag">npm</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/orm/" rel="tag">orm</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/properties/" rel="tag">properties</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/thymeleaf/" rel="tag">thymeleaf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/xml/" rel="tag">xml</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/yarn/" rel="tag">yarn</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F/" rel="tag">代理模式</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/%E6%B3%9B%E5%9E%8B/" rel="tag">泛型</a><span class="tag-list-count">1</span></li></ul>
        </div>
    </div>


            
                
    <div class="widget-wrap widget-float">
        <h3 class="widget-title">标签云</h3>
        <div class="widget tagcloud">
            <a href="/hexo/tags/AOP/" style="font-size: 10px;">AOP</a> <a href="/hexo/tags/Collection/" style="font-size: 10px;">Collection</a> <a href="/hexo/tags/DI/" style="font-size: 10px;">DI</a> <a href="/hexo/tags/HashMap/" style="font-size: 10px;">HashMap</a> <a href="/hexo/tags/HashTabel/" style="font-size: 10px;">HashTabel</a> <a href="/hexo/tags/IOC/" style="font-size: 10px;">IOC</a> <a href="/hexo/tags/List/" style="font-size: 10px;">List</a> <a href="/hexo/tags/Map/" style="font-size: 10px;">Map</a> <a href="/hexo/tags/Set/" style="font-size: 10px;">Set</a> <a href="/hexo/tags/TreeMap/" style="font-size: 10px;">TreeMap</a> <a href="/hexo/tags/idea/" style="font-size: 20px;">idea</a> <a href="/hexo/tags/jdbc/" style="font-size: 20px;">jdbc</a> <a href="/hexo/tags/jpa/" style="font-size: 10px;">jpa</a> <a href="/hexo/tags/mybatis/" style="font-size: 20px;">mybatis</a> <a href="/hexo/tags/mysql/" style="font-size: 10px;">mysql</a> <a href="/hexo/tags/npm/" style="font-size: 10px;">npm</a> <a href="/hexo/tags/orm/" style="font-size: 10px;">orm</a> <a href="/hexo/tags/properties/" style="font-size: 10px;">properties</a> <a href="/hexo/tags/thymeleaf/" style="font-size: 10px;">thymeleaf</a> <a href="/hexo/tags/xml/" style="font-size: 15px;">xml</a> <a href="/hexo/tags/yarn/" style="font-size: 10px;">yarn</a> <a href="/hexo/tags/%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F/" style="font-size: 10px;">代理模式</a> <a href="/hexo/tags/%E6%B3%9B%E5%9E%8B/" style="font-size: 10px;">泛型</a>
        </div>
    </div>


            
                
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">链接</h3>
        <div class="widget">
            <ul>
                
                    <li>
                        <a target="_blank" rel="noopener" href="http://hexo.io">Hexo</a>
                    </li>
                
                    <li>
                        <a target="_blank" rel="noopener" href="https://github.com">Github</a>
                    </li>
                
                    <li>
                        <a target="_blank" rel="noopener" href="https://baidu.com">Baidu</a>
                    </li>
                
            </ul>
        </div>
    </div>


            
        
    </div>
</aside>

                </div>
            </div>
        </div>
        <footer id="footer">
    <div class="container">
        <div class="container-inner">
            <a id="back-to-top" href="javascript:;"><i class="icon fa fa-angle-up"></i></a>
            <div class="credit">
                <h1 class="logo-wrap">
                    <a href="/hexo/" class="logo"></a>
                </h1>
                <p>&copy; 2021 XiaoHuang</p>
                
                <p>Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>.</p>
                
            </div>
            <div class="footer-plugins">
              
    


            </div>
        </div>
	</div>
</footer>


    </div>
    
    
    <script>
    var disqus_shortname = 'hexo-theme-hueman';
    
    
    var disqus_url = 'https://hchdream.gitee.io/hexo/2020/05/14/Collection%E9%9B%86%E5%90%88%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E5%8F%8A%E4%BB%A3%E7%A0%81/';
    
    (function() {
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
    </script>





    
        
<script src="/hexo/libs/lightgallery/js/lightgallery.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-thumbnail.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-pager.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-autoplay.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-fullscreen.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-zoom.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-hash.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-share.min.js"></script>

        
<script src="/hexo/libs/lightgallery/js/lg-video.min.js"></script>

    
    
        
<script src="/hexo/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>

    
    

    
    
    



<!-- Custom Scripts -->

<script src="/hexo/js/main.js"></script>


</body>
</html>
